package com.example.exceldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.exceldemo.entity.TUser;
import com.example.exceldemo.entity.UserExcel;
import com.example.exceldemo.mapper.TUserMapper;
import com.example.exceldemo.mapper.UserMapper;
import com.example.exceldemo.service.ExportService;
import com.example.exceldemo.write.BatchExport;
import com.example.exceldemo.write.BatchExportToWeb;
import com.example.exceldemo.write.ExcelWriteTool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yanhongwei
 * @Date: 2023/07/13 16:14:32
 * @Description: TODO
 */
@Service
@Slf4j
public class ExportServiceImpl implements ExportService {

    @Autowired
    ExcelWriteTool excelWriteTool;

    @Autowired
    BatchExport batchExport;

    @Autowired
    BatchExportToWeb batchExportToWeb;

    @Autowired
    UserMapper userMapper;

    @Autowired
    TUserMapper tUserMapper;

    @Override
    public String exportExcel02(HttpServletResponse response) {
        List<TUser> userExcelList = tUserMapper.selectList(null);
        long l1 = System.currentTimeMillis();
        excelWriteTool.exportDictData(response, TUser.class, "export-test", userExcelList);
        long l2 = System.currentTimeMillis();
        return "数据导出成功,花费时间" + (l2 - l1) + "毫秒";
    }

    @Override
    public String exportDictModel(HttpServletResponse response) {

        excelWriteTool.exportDictData(response, UserExcel.class, "学生信息", null);
        return "模板下载成功";
    }

    @Override
    public String batchExport() {
        batchExport.exportBatch();
        return "导出成功";
    }

    @Override
    public String batchExport(HttpServletResponse response) {

        QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
        Long count = tUserMapper.selectCount(queryWrapper);

        batchExportToWeb.exportBatch(response, TUser.class, new ArrayList<>(), "ceshishuju", count);
        return "导出成功";
    }
}
